package com.aop.springbootaop.contoller;

import com.aop.springbootaop.entity.User;
import com.aop.springbootaop.repository.UserRepository;
import com.aop.springbootaop.service.UserMongoService;
import com.aop.springbootaop.service.UserMysqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMysqlService userMysqlService;
    @Autowired
    private UserMongoService userMongoService;

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users/mysql/{id}")
    public User getUserById(@PathVariable Long id) {
        return userMysqlService.getUserById(id);
    }

    @GetMapping("/users/mysql/{pageNo}/{pageSize}")
    public List<User> selectUserByPage(@PathVariable int pageNo, @PathVariable int pageSize) {
        return userMysqlService.selectUserByPage(pageNo,pageSize);
    }

    @GetMapping("/users/mongo/{name}")
    public User selectByName(@PathVariable String name) {
        return userMongoService.selectByName(name);
    }

    @GetMapping("/users/mongo")
    public List<User> findAll() {
        return userMongoService.findAll();
    }

    @GetMapping("/users/mongo-template")
    public List<User> findAllFromMongoTemplate() {
        return userMongoService.findAllFromMongoTemplate();
    }

    @GetMapping("/users/mongo/add/{number}")
    public boolean selectByName(@PathVariable int number) {
        for (int i = 1; i < number; i++) {
            User user = new User();
            user.setName("user"+i);
            user.setAge(i);
            user.setId((long) i);
            userRepository.save(user);
        }
        return true;
    }


}
